
export default class Entity {
    // private _world = null;
    // private t_events = new Map();
    // private m_events = new Map();

    constructor(id, world) {
        this.id = id;
        this._world = world;
        this.t_events = new Map();
        this.m_events = new Map();
    }

    _serv_modify(key, value) {
        this[key] = value;
        let _events = this.m_events.get(key);
        if (_events) {
            _events.forEach(cb => {
                cb(value);
            });
        }
    }

    _serv_trigger(key, params) {
        let serv_k = `_serv_${key}`;
        if (typeof this[serv_k] === 'function') {
            this[serv_k](params);
        }
        let _events = this.t_events.get(key);
        if (_events) {
            _events.forEach(cb => {
                cb(params);
            });
        }
    }

    onModifyEvent(key, cb) {
        let _events = this.m_events.get(key);
        if (!_events) {
            _events = [];
            this.m_events.set(key, _events);
        }
        _events.push(cb);
    }

    onTriggerEvent(key, cb) {
        let _events = this.t_events.get(key);
        if (!_events) {
            _events = [];
            this.t_events.set(key, _events);
        }
        _events.push(cb);
    }

    async trigger(key, params) {
        if(!this._world) return false

        cc.log("trigger,key:",key," params:",JSON.stringify(params))
        return await this._world.trigger(key, params || []);
        // return true
    }

    clearListeners(){
        this.t_events.clear();
        this.m_events.clear();
    }

    onDestroy() {
        this._world = null;
        this.clearListeners();
    }

    en_class() {
        return 'Entity';
    }
}